package com.youdianstar.app.util

import com.nimbusds.jwt.JWTParser
import com.nimbusds.jwt.SignedJWT
import java.text.ParseException


/**
 * File: JwtParserUtil
 * Author: Administrator Create: 2025/2/18 0018 17:33
 * Changes (from 2025/2/18 0018)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
object JwtParserUtil {
    fun parseIdToken(idToken: String) :String {
        var accountId = ""
        try {
            // 解析 JWT
            val jwt = JWTParser.parse(idToken)
            if (jwt is SignedJWT) {
                // 获取 JWT 的声明集
                val claims = jwt.jwtClaimsSet.claims
                // 遍历所有声明
                for ((key, value) in claims) {
                    if (key == "sub")
                        accountId = value.toString()
//                    println("$key: $value")
                }
            }
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return accountId
    }
}